package com.cy.pj.modules.controller;

import com.cy.pj.common.vo.JsonResult;
import com.cy.pj.common.web.domain.BaseController;
import com.cy.pj.modules.entity.Client;
import com.cy.pj.modules.service.IClientService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 用户基础信息Controller
 *
 * @author kiven
 * @date 2023-02-09
 */
@Controller
@RequestMapping("/client")
public class ClientController extends BaseController
{
    private String prefix = "modules/client";

    @Autowired
    private IClientService clientService;

    @RequiresPermissions("client:view")
    @GetMapping()
    public String client()
    {
        return prefix + "/client";
    }

    /**
     * 查询用户基础信息列表
     */
    @RequiresPermissions("client:list")
    @GetMapping("/list")
    @ResponseBody
    public JsonResult list(Client client,Integer pageCurrent)
    {
        return new JsonResult(clientService.selectClientList(client,pageCurrent));
    }

    /**
     * 查询用户基础信息列表
     */
    @GetMapping("/id")
    @ResponseBody
    public JsonResult findClientById(Long id)
    {
        return new JsonResult(clientService.selectClientById(id));
    }

    ///**
    // * 导出用户基础信息列表
    // */
    //@RequiresPermissions("pj:client:export")
    //@Log(title = "用户基础信息", businessType = BusinessType.EXPORT)
    //@PostMapping("/export")
    //@ResponseBody
    //public AjaxResult export(Client client)
    //{
    //    List<Client> list = clientService.selectClientList(client);
    //    ExcelUtil<Client> util = new ExcelUtil<Client>(Client.class);
    //    return util.exportExcel(list, "client");
    //}

    /**
     * 新增用户基础信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户基础信息
     */
    @RequiresPermissions("client:add")
    @PostMapping("/add")
    @ResponseBody
    public JsonResult addSave(Client client)
    {
        client.setRegisterTime(new Date());
        client.setScore(0L);
        return new JsonResult(clientService.insertClient(client));
    }

    /**
     * 修改用户基础信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Client client = clientService.selectClientById(id);
        mmap.put("client", client);
        return prefix + "/client_edit";
    }

    /**
     * 修改保存用户基础信息
     */
    @RequiresPermissions("client:update")
    @PostMapping("/edit")
    @ResponseBody
    public JsonResult editSave(Client client)
    {
        return new JsonResult(clientService.updateClient(client));
    }

    /**
     * 删除用户基础信息
     */
    @RequiresPermissions("client:delete")
    @PostMapping( "/remove")
    @ResponseBody
    public JsonResult remove(String ids)
    {
        return new JsonResult(clientService.deleteClientByIds(ids));
    }
}
